//
//  AnimateViewController.m
//  MyReader
//
//  Created by hh on 15/12/11.
//  Copyright © 2015年 hh. All rights reserved.
//

#import "AnimateViewController.h"

@interface MyLayer : CALayer
@property (assign, nonatomic) CGFloat progress;
@end

@implementation MyLayer

+ (BOOL)needsDisplayForKey:(NSString *)key
{
    if ([key isEqualToString:@"progress"]) {
        return YES;
    }
    return [super needsDisplayForKey:key];
}

- (void)drawInContext:(CGContextRef)ctx
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    CGFloat lineWidth = 3;
    CGFloat radius = 50 - lineWidth/2;
    CGPoint center = CGPointMake(50, 50);
    CGFloat origStart = M_PI * 2;
    CGFloat origEnd = 0;
    CGFloat destStart = M_PI * 2 + 0.3;
    CGFloat destEnd = 0.3;
    
    CGFloat curOrig = origStart - _progress * (origStart - origEnd);
    CGFloat curDest = destStart - _progress * (destStart - destEnd);
    [path addArcWithCenter:center radius:radius startAngle:curOrig endAngle:curDest clockwise:NO];
    CGContextAddPath(ctx, path.CGPath);
    CGContextSetLineWidth(ctx, lineWidth);
    CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
    CGContextStrokePath(ctx);
}

@end



@interface AnimateViewController () <UIGestureRecognizerDelegate>
{
    MyLayer *layer;
    UIView *view;
    
    CGFloat lastScale;
    CGFloat lastRotate;
}
@end

@implementation AnimateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setTitle:@"开始动画" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(startAnimate:) forControlEvents:UIControlEventTouchUpInside];
    [btn sizeToFit];
    btn.center = CGPointMake(self.view.frame.size.width/2.0, 100);
    [self.view addSubview:btn];
    
    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setTitle:@"停止动画" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(stopAnimate:) forControlEvents:UIControlEventTouchUpInside];
    [btn sizeToFit];
    btn.center = CGPointMake(self.view.frame.size.width/2.0, 400);
    [self.view addSubview:btn];
    
    layer = [MyLayer new];
    layer.frame = CGRectMake(self.view.center.x - 50, 200, 100, 100);
    layer.progress = 1;
    [self.view.layer addSublayer:layer];
    
    view = [[UIView alloc] initWithFrame:CGRectMake(10, 200, 100, 100)];
    view.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:view];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(onPan:)];
    [view addGestureRecognizer:pan];
    
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(onPinch:)];
    [view addGestureRecognizer:pinch];
    
    UIRotationGestureRecognizer *rot = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(onRotate:)];
    [view addGestureRecognizer:rot];
    
    pinch.delegate = self;
    rot.delegate = self;
    
    lastScale = 1.0;
    lastRotate = 0;
    
    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"progress"];
    ani.duration = 1;
    ani.fromValue = @(0);
    ani.toValue = @(1);
    ani.repeatCount = 100;
    [layer addAnimation:ani forKey:@"my"];
    [self pauseLayer];
}

- (void)onPan:(UIPanGestureRecognizer *)pan
{
    CGPoint p = [pan translationInView:pan.view];
//    CGRect rect = pan.view.frame;
//    rect.origin.x += p.x;
//    rect.origin.y += p.y;
//    pan.view.frame = rect;
    pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
    
    [pan setTranslation:CGPointZero inView:pan.view];
}

- (void)onPinch:(UIPinchGestureRecognizer *)pinch
{
    CGFloat scale = pinch.scale - lastScale + 1;
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, scale, scale);
    lastScale = pinch.scale;
}

- (void)onRotate:(UIRotationGestureRecognizer *)rotate
{
    CGFloat rot = rotate.rotation - lastRotate;
    rotate.view.transform = CGAffineTransformRotate(rotate.view.transform, rot);
    lastRotate = rotate.rotation;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}


- (void)startAnimate:(id)sender
{
    [self resumeLayer];
//    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"progress"];
//    ani.duration = 1;
//    ani.fromValue = @(0);
//    ani.toValue = @(1);
//    ani.repeatCount = 100;
//    ani.fillMode = kCAFillModeForwards;
//    [layer addAnimation:ani forKey:@"my"];
    
//    CGAffineTransform trans = view.transform;
//    [UIView animateWithDuration:0.6 animations:^{
//        view.transform = CGAffineTransformMakeRotation(M_PI);
//    } completion:^(BOOL finish){
//        view.transform = trans;
//    }];
}

- (void)stopAnimate:(id)sender
{
    [self pauseLayer];
//    [layer removeAnimationForKey:@"my"];
}

-(void)pauseLayer
{
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}

-(void)resumeLayer
{
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
